﻿using ReadWriteCsv;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace StockAnomalyDetector
{
    // 跳瘙抓住那些连续十分钟及以上，价格以3%偏离基准线震荡，每分钟平均有五次的家伙
    // 输出文件格式， 代码，日期，震荡开始时间，震荡结束时间
    class Flea
    {
        public double avLevel;
        private const double DIVIATION = 0.03;

        private int oscilating;
        private decimal sliceSum;
        private bool uping;
        private double highLevel;
        private double lowLevel;
        private List<int> oscilatingTime ;
        private decimal quantityCount;
        public static List<string> exceptionList = new List<string>() { "stockCode,date,startTime,endTime"};
        public static CsvFileWriter writer = new CsvFileWriter(@"D:\股票数据\" + "Flea" + ".csv");

        public Flea()
        {
            quantityCount = 0;
            oscilating = 0;
            sliceSum = 0;
            avLevel = 0;
            uping = false;
            highLevel = 100000;
            lowLevel = -1;
            oscilatingTime = new List<int>();
        }

        public void update(double price, int quantity)
        {
            sliceSum += Convert.ToDecimal(price*quantity);
            quantityCount += quantity;
            // 会自动跳过第一个时间片
            if (price > highLevel && !uping)
            {
                oscilating++;
                uping = true;
            }

            if (price < highLevel && uping)
            {
                oscilating++;
                uping = false;
            }
        }
        public void analyzeSlice(Clock clock)
        {
            if (oscilating > 19)
                oscilatingTime.Add(clock.toInt32());

            avLevel = Convert.ToDouble(sliceSum / quantityCount);
            highLevel = avLevel * (1 + DIVIATION);
            lowLevel = avLevel * (1 - DIVIATION);
        }

        public void reset()
        {
            oscilating = 0;
            sliceSum = 0;
            uping = false;
            quantityCount = 0;
        }

        public void analyzeRecord(string sourceName)
        {
            // 取那些疯狂震荡十分钟及以上的，每只股票每天返回最早的一次最长震荡时间
            int beg = 0;
            int end = 0;
            List<int> currentDur = new List<int>();
            int diff = 0;
            int currentDiff = 0;

            for (int i = 1; i < oscilatingTime.Count; i++)
            {
                if (oscilatingTime[i] - oscilatingTime[i - 1] == 500 || oscilatingTime[i] - oscilatingTime[i - 1] == 5500)
                {
                    currentDiff++;
                    currentDur.Add(oscilatingTime[i]);
                    if (currentDiff > diff)
                    {
                        diff = currentDiff;
                        beg = Clock.turnBack(currentDur[0]);
                        end = currentDur[diff-1];
                    }
                }
                else
                {
                    currentDur.Clear();
                    currentDiff = 0;
                }
            }
            if(diff>=1)
            {
                Record record = new Record
                {
                    stockcode = Convert.ToString(Regex.Match(sourceName, @"[A-Z]{2}\d{6}")),
                    date = Convert.ToString(Regex.Match(sourceName, @"\d{8}")),
                    startTime = beg,
                    endTime = end
                };
                Console.WriteLine("Flea： " + record);
                exceptionList.Add(record.ToString());
            }

            
        }
    }
}
